# channel_dict = {
#     'channel_name': {
#         'first_category': [
#             {'id': 'value', 'name': 'value', 'subs': 'value', },  # 每个列表代表一个一级分类
#             {'id': 'value', 'name': 'value', 'subs': 'value', },
#             {'id': 'value', 'name': 'value', 'subs': 'value', },
#             {'id': 'value', 'name': 'value', 'subs': 'value', },
#         ],  # 存放一级分类对象
#     }
# }
from apps.index.models import JobCategory, JobChannelGroup, JobChannel


def get_categories():
    channels = JobChannelGroup.objects.all()
    channel_dict = {}
    for channel in channels:
        channel_dict[channel.name] = {}
        channel_dict[channel.name]["first_category"] = []
        channel_tb = JobChannel.objects.filter(group=channel)

        # print(channel.name)
        for channel_cate in channel_tb:
            first_category = JobCategory.objects.get(id=channel_cate.category_id)
            # print(category.name)
            first_category_dict = {
                'id': first_category.id,
                'name': first_category.name,
                'subs': [],
            }

            second_categories = JobCategory.objects.filter(parent=first_category)
            # print(second_categories)
            for second_category in second_categories:
                # print(second_category.name)
                second_category_dict = {
                    'id': second_category.id,
                    'name': second_category.name
                }
                first_category_dict['subs'].append(second_category_dict)
            channel_dict[channel.name]['first_category'].append(first_category_dict)
    return channel_dict

